package com.gxuwz.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.Goods;
import com.gxuwz.restaurant.mapper.GoodsMapper;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wine")
public class WineController {
    @Resource
    GoodsMapper goodsMapper;


    @GetMapping("/select_available")   //get用来查询
    public Result<?> findPageselect_available(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String type,
                              @RequestParam(defaultValue = "") Integer days){
        System.out.println("执行显示酒水信息+dddd");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }
        if (StrUtil.isNotBlank(String.valueOf(days))){
            wrapper.gt(Goods::getDays,days);
        }

        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);

        System.out.println(goodsPage.getRecords());
        return Result.success(goodsPage );
    }

    @GetMapping("/select")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String type,
                              @RequestParam(defaultValue = "") Integer days){
        System.out.println("执行显示酒水信息+dddd");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }


        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);

        System.out.println(goodsPage.getRecords());
        return Result.success(goodsPage );
    }

    @PostMapping ("/add")//post用来新增
    public Result<?> save(@RequestBody Goods goods){
        System.out.println("执行添加");
       goods.setCategory("0");//设置为酒水类
        goodsMapper.insert(goods);

        return Result.success();
    }

    @PutMapping ("update")//put用来更新
    public Result<?> update (@RequestBody Goods goods){
        System.out.println("执行更新");

        goodsMapper.updateById(goods);

        return Result.success();


    }

    @DeleteMapping("/{id}") //DELETE用来删除
    public Result<?> delete(@PathVariable long id){
        System.out.println("执行删除方法");
        goodsMapper.deleteById(id);

        return Result.success();


    }

    //酒水库存管理
    @GetMapping("/selectStock")   //get用来查询
    public Result<?> findPageStock(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String type){
        System.out.println("执行显示菜品信息");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        wrapper.lt(Goods::getStock,50);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }

        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);


        return Result.success(goodsPage );
    }
    @GetMapping("/selectStock1")   //get用来查询
    public Result<?> findPageStock1(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "") String type){
        System.out.println("执行显示菜品信息");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        wrapper.between(Goods::getStock,50,200);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }
        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);
        return Result.success(goodsPage );
    }
    @GetMapping("/selectStock2")   //get用来查询
    public Result<?> findPageStock2(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search,
                                    @RequestParam(defaultValue = "") String type){
        System.out.println("执行显示菜品信息");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        wrapper.ge(Goods::getStock,200);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }
        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);
       return Result.success(goodsPage );
    }
    //======================================
    @GetMapping("/selectStock3")   //get用来查询
    public Result<?> findPageStock3(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search,
                                    @RequestParam(defaultValue = "") String type){
        System.out.println("执行显示菜品信息");
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getCategory,"0");
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getGoodsname,search);

        }else if (StrUtil.isNotBlank(type)){
            wrapper.like(Goods::getType,type);
        }

        Page<Goods> goodsPage =  goodsMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);

        for(Goods goods:goodsPage.getRecords()){
            Date nowDate = new Date();
            long from1 = nowDate.getTime();
            long to1 = goods.getExpirationTime().getTime();
            int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
            goods.setDays(days);
          goodsMapper.updateById(goods);
        }

        return Result.success(goodsPage);
    }

    @GetMapping("/getNowTime")   //get用来查询
    public Result<?> getNowTime(){
        Date nowDate = new Date();
        SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return Result.success(a.format(nowDate));
    }
}
